package com.wnzt.baszh.dao.impl;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.wnzt.baszh.dao.IDicDao;

@Repository
public class DicDaoImpl implements IDicDao{
	
	@Autowired
	private JdbcTemplate jdbcTemplate;

	@Autowired
	private SessionFactory sessionFactory;

	@Override
	public List findICD11code(String param, int pageNum, int pageSize) {
		String where = "";
		if (param != null && !param.equals(""))
			where = " AND(FICDM LIKE '%" + param + "%' OR FJBNAME LIKE '%"
					+ param + "%' OR FZJC LIKE '%" + param + "%')";

		String sql = "SELECT TOP " + pageSize
				+ " * FROM Ticd WHERE FICDVERSION='11'" + where
				+ " AND FID NOT IN (SELECT TOP " + pageSize * (pageNum - 1)
				+ " FID FROM Ticd WHERE FICDVERSION='11'" + where + ")";
		return this.jdbcTemplate.queryForList(sql);
	}

	@Override
	public Integer findICDTotal(String param) {
		String sql = "SELECT count(1) FROM Ticd WHERE FICDVERSION='11'";
		if (param != null && !param.equals(""))
			sql += " AND(FICDM LIKE '%" + param + "%' OR FJBNAME LIKE '%"
					+ param + "%' OR FZJC LIKE '%" + param + "%')";
		return this.jdbcTemplate.queryForInt(sql);
	}

	@Override
	public List findTOPERATEcode(String param, int pageNum, int pageSize) {
		String where = "";
		if (param != null && !param.equals(""))
			where = " AND(FOPCODE LIKE '%" + param + "%' OR FOPNAME LIKE '%"
					+ param + "%' OR FZJC LIKE '%" + param + "%')";

		String sql = "SELECT TOP " + pageSize + " * FROM TOPERATE WHERE 1=1 "
				+ where + " AND FID NOT IN (SELECT TOP " + pageSize
				* (pageNum - 1) + " FID FROM TOPERATE WHERE 1=1" + where + ")";
		return this.jdbcTemplate.queryForList(sql);
	}

	@Override
	public Integer findTOPERATETotal(String param) {
		String sql = "SELECT COUNT(1) FROM TOPERATE WHERE 1=1";
		if (param != null && !param.equals(""))
			sql += " AND(FOPCODE LIKE '%" + param + "%' OR FOPNAME LIKE '%"
					+ param + "%' OR FZJC LIKE '%" + param + "%')";
		return this.jdbcTemplate.queryForInt(sql);
	}

	@Override
	public List findStandardByCode(String code) {
		String sql = "SELECT * FROM TSTANDARDMX WHERE FCODE='" + code
				+ "' ORDER BY FPX";
		return this.jdbcTemplate.queryForList(sql);
	}

	@Override
	public List findTworkroom() {
		String sql = "select obj.* from Tworkroom obj where obj.fcur = 1 and obj.ftype=2 order by obj.ftype,obj.fpx";
		return this.jdbcTemplate.queryForList(sql);
	}

	@Override
	public List findBambMx(String mainid) {
		return sessionFactory.getCurrentSession()
				.createQuery("from TbasxMx where fmbid='" + mainid + "'")
				.list();
	}

	
}
